﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LGSVR.Common;

namespace LGSVR_WEB.terminal
{
    public partial class SysParam : DataBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserID"] == null)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "out", "<script>alert('系统超时或服务器故障，请退出重新登录！');window.parent.parent.navigate('../Logout.aspx');</script>");
            }
            if (!IsPostBack)
            {
                PageInit();
            }
        }

        private void PageInit()
        {
            dropLogGrade.Items.Add(new ListItem("1", "1"));
            dropLogGrade.Items.Add(new ListItem("2", "2"));
            dropLogGrade.Items.Add(new ListItem("3", "3"));

            Dictionary<string, string> dicSingle;


            dicSingle = GetSigle("sysParam", "");

            if (dicSingle != null)
            {
                dropLogGrade.SelectedValue = dicSingle["logGrade"];
                txtRetryTimes.Text = dicSingle["retryTimes"];
                txtSendTimes.Text = dicSingle["SendTimes"];
                txtAcceptOutTime.Text = dicSingle["acceptOutTime"];
                txtSendDepTime.Text = dicSingle["sendDepTime"];
                txtTaskOutTime.Text = dicSingle["taskOutTime"];
                txtUserDownTime.Text = dicSingle["userDownTime"];
                txtCardDownTime.Text = dicSingle["cardDownTime"];
                hidID.Value = dicSingle["ID"];
            }
            else
            {
                MsgContent = ShowMessage("数据加载失败。", MessageType.Error, true, false);
            }

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {            
            Dictionary<string, string> dicParam= new Dictionary<string, string>();             

            dicParam.Add("logGrade", dropLogGrade.SelectedValue);
            dicParam.Add("retryTimes", txtRetryTimes.Text);
            dicParam.Add("SendTimes", txtSendTimes.Text);
            dicParam.Add("acceptOutTime", txtAcceptOutTime.Text);
            dicParam.Add("sendDepTime", txtSendDepTime.Text);
            dicParam.Add("taskOutTime", txtTaskOutTime.Text);
            dicParam.Add("userDownTime", txtUserDownTime.Text);
            dicParam.Add("cardDownTime", txtCardDownTime.Text);
            // 编辑
            if (!string.IsNullOrEmpty(Request.QueryString["action"]) && Request.QueryString["action"].ToLower() == "edit")
            {
                if (hidID.Value!="0")
                {
                    Edit("sysParam", dicParam, "ID=" + hidID.Value);

                    if (Result > 0)
                    {
                        MsgContent = ShowMessage("恭喜您，更新成功！", MessageType.Success, false, true);
                        NoRefresh();
                    }
                    else
                    {
                        if (Result == -100)
                        {
                            MsgContent = ShowMessage("对不起，操作失败，程序参数异常。", MessageType.Error, false, true);
                        }
                        else
                        {
                            MsgContent = ShowMessage("对不起,操作失败，请重试。", MessageType.Error, false, true);
                        }
                    }
                }
            }
            else  // 新增
            {
                Insert("sysParam", dicParam);

                if (Result > 0)
                {
                    MsgContent = ShowMessage("恭喜您，添加成功！", MessageType.Success, false, true);
                    NoRefresh();
                }
                else
                {
                    MsgContent = ShowMessage("对不起,操作失败，请重试。", MessageType.Error, false, true);
                }
            }
        }
    }
}